package com.zshield.entry;

import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.zshield.util.Etime;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

public class FileSystemTreeEntry implements EntryInterface<FileSystemTreeEntry> {
    @SerializedName("SENSOR_ID")
    private String sensorId;
    @SerializedName("FORMAT")
    private String format;
    @SerializedName("FILE_NAME")
    private String fileName;
    @SerializedName("FILE_EXT")
    private String fileExt;
    @SerializedName("ACCESS_FORMAT")
    private Set<String> accessFormat;
    @SerializedName("FS_ACTION")
    private Set<String> fsAction;
    @SerializedName("EXE_NAME")
    private Set<String> exeName;
    @SerializedName("USERNAME_EX")
    private Set<String> usernameEx;
    @SerializedName("FILE_GUID")
    private Set<String> fileGuid;
    @SerializedName("TIME")
    private String time;
    @SerializedName("ORIGINAL_FILE_NAME")
    private String origFileName;
    @SerializedName("DAY_TIME")
    private String dayTime;
    @SerializedName("PTYPE")
    private String ptype;
    @SerializedName("@timestamp")
    private String timeStamp;
    @SerializedName("TIME_hms")
    private String timeHms;
    @SerializedName("FILE_NAME_LOWER")
    private String fileNameLower;
    @SerializedName("FILE_SIZE")
    private Long fileSize;
    @SerializedName("IP")
    private Set<String> ip;
    @SerializedName("MAC")
    private Set<String> mac;
    @SerializedName("COMPUTER")
    private Set<String> computer;

    public FileSystemTreeEntry (JsonObject obj,String fileSysAction,String accFormat,String fg) {
        sensorId = obj.get("SENSOR_ID").getAsString();
        format = "PRE_COMPUTE";
        fileName = obj.get("FILE_NAME").getAsString();
        fileSize = obj.get("FILE_SIZE").getAsLong();
        String ext = obj.get("FILE_EXT").getAsString();
        fileExt = ext.contains(",") ? ext.split(",")[0] : ext;
        origFileName = obj.get("ORIGINAL_FILE_NAME").getAsString();
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        timeHms = ldt.format(Etime.TIME_FORMATTER);
        ptype = "FilesysTree";
        accessFormat = new HashSet<>();
        if (!accFormat.equals("")){
            accessFormat.add(accFormat);
        }
        fsAction = new HashSet<>();
        fsAction.add(fileSysAction);
        String en = obj.get("EXE_NAME").getAsString();
        exeName = new HashSet<>();
        exeName.add(en);
        String uname = obj.get("USERNAME_EX").getAsString();
        usernameEx = new HashSet<>();
        usernameEx.add(uname);
        fileGuid = new HashSet<>();
        fileGuid.add(fg);
        String ips = obj.get("IP").getAsString();
        ip = new HashSet<>();
        ip.add(ips);
        String macs = obj.get("MAC").getAsString();
        mac = new HashSet<>();
        mac.add(macs);
        String computers = obj.get("COMPUTER").getAsString();
        computer = new HashSet<>();
        computer.add(computers);
        if (fileSysAction.equals("Copy") || fileSysAction.equals("Move") || fileSysAction.equals("Rename")){
            fileName = obj.get("FILE_NAME2").getAsString();
        }
        fileNameLower = fileName.toLowerCase();
    }

    @Override
    public void update(FileSystemTreeEntry that) {
        accessFormat.addAll(that.accessFormat);
        fsAction.addAll(that.fsAction);
        exeName.addAll(that.exeName);
        usernameEx.addAll(that.usernameEx);
        fileGuid.addAll(that.fileGuid);
        ip.addAll(that.ip);
        mac.addAll(that.mac);
        computer.addAll(that.computer);
        fileSize = that.fileSize;
    }

    @Override
    public String getDayTime() {
        return this.dayTime;
    }

    @Override
    public String getDocId() {
        return "FilesysTree~" + fileName + "~" + sensorId;
    }

}
